/**
 * Created by Administrator on 2016/2/16.
 */


$(document).ready(function(){
    $("#addRoleForm,#addUserGroup").validationEngine('attach',{
        promptPosition:'bottomLeft'
    });//初始化查询条件的验证
});

function searchByParam(){
    var p=$("#queryRolesByParam").serializeFormJSON();
    $("#dataTable").selectDataAfterSetParm(p);
}

//删除
function delRole(){

    var url=p_path+"/settings/prvlg/role/delRole1.json";
    var data1=myTableOpt_.getCheckBoxValues($("#dataTable"));
    if (data1==null || data1.length==0){
        alert("请选择至少一个项目")
        return;
    }
    if (!confirm("是否要删除选中的"+data1.length+"条记录?")){
        return;
    }
    var data = {"dataIds":data1};
    $().invoke(url,data,function(m,r){
        flashMessage_(r)
        $("#dataTable").refresh();
    })
}
function delARole(id){
    if (isEmpty(id)){
        alert("没有相关参数")
        return;
    }

    if (!confirm("是否要删除该记录?")){
        return;
    }
    var url=p_path+"/settings/prvlg/role/delRole1.json";
    var data = {"rid":id};
    $().invoke(url,data,function(m,r){
        flashMessage_(r)
        $("#dataTable").refresh();
    })
}


//新增
function addRole(){
    if (isEmpty($("input[name=roleName]").val())){
        alert("角色名称不能为空");
        return;
    }
    if (isEmpty($("input[name=roleCode]").val())){
        alert("角色编号不能为空");
        return;
    }
    var vR=$("#addRoleForm").validationEngine("validate");
    if (!vR){
        return;
    }
    var url=p_path+"/settings/prvlg/role/insertRole1.json";
    var data=$("#addRoleForm").serialize();
    $().invoke(url,data,function(m,r){
        flashMessage_(r);
        $('#add-js').modal('hide');
        $("#dataTable").refresh();
    })
}

/**
 * 批量更新状态
 */
function batchUpdateRoleStat(st){
    var m="";
    if (st==0){
        m="启用";
    }else if ( st==1){
        m="禁用";
    }
    if (!confirm("是否要"+m+"选中项目?")){
        return;
    }
    var data1=myTableOpt_.getCheckBoxValues($("#dataTable"));
    if (data1==null || data1.length==0){
        alert("请选择至少一个项目")
        return;
    }
    var data = {"dataIds":data1,"STATE":st};
    updateRoleStat(null,null,data);
}

/**
 * 更改状态
 * @param id
 * @param st
 * @param data
 */
function updateRoleStat(id,st,data){
    if (isEmpty(id)&&isEmpty(data)){
        alert("缺少id参数")
        return;
    }
    var url=p_path+"/settings/prvlg/updateState1.json";
    if (st==null||st==''||st=='1'||st==1){
        st=0;
    }else {
        st=1;
    }
    if (data==null){
        data={"id":id,"STATE":st}
    }
    $().invoke(
        url,
        data,
        [function(m,r){
            flashMessage_(r);
            $("#dataTable").refresh();
        },function(m,r){
            alert(r);
            $("#dataTable").refresh();
        }]
    )
}

function clearAddRole(){
    initMyForm('addRoleForm');
}

var roleTableOpt={
    getOpt:function(json){
        var html = "<li style='list-style-type:none;' class='w15 no-right'>" ;
            html += "<a href='javascript:void(0);' class='edit_00' onclick=modifyRoleDiag(this)></a>";
        html += "<a href='javascript:void(0);'  class='delete_00' onclick=delARole('"+json.ID_+"')></a>" +
            "<a href='#shouquanlayer' data-toggle=\"modal\"  class='shouquan_00' onclick=getMenuList('"+json.ID_+"')></a>" +
            "</li>";
        return html;
    }
};

var roleId_souquan=null;
function saveRoleMenu(){
    if (isEmpty(roleId_souquan)){
        alert("没有获取到roleid，无法授权")
        return;
    }
    var data1=myTableOpt_.getCheckBoxValues($("#menuList"));
    var data = {"dataIds":data1,"roleId":roleId_souquan};
    var url=p_path+"/settings/prvlg/role/saveRoleMenu.json";
    $().invoke(
        url,
        data,[function(m,r){
            alert(r);
            roleId_souquan=null;
            $('#shouquanlayer').modal('hide');
        },
        function(m,r){
            alert(r);
            //roleId_souquan=null;
        }]
    )
}

/**
 * 获取所有菜单列表
 */
function getMenuList(roleId){
    roleId_souquan=roleId;
    var col=[
        {title:myTableOpt_.getCheckBox(null),name:"ID",width:"5%",align:"center",format:function(json){
            return myTableOpt_.getCheckBox(json.id,json.parentId,"checkedMenuBox(this,'menuList')")}
        },
        {title:"菜单",name:"showName",width:"20%",align:"center"},
        {title:"菜单编号",name:"code",width:"10%",align:"left"},
        {title:"路径",name:"url",width:"20%",align:"center"},
        {title:"图片",name:"imgUrl",width:"20%",align:"center"}
    ];
    var url=p_path+"/settings/prvlg/menu/queryMenuList.json";
    $("#menuList").initTable({
        url: url,
        columnData:col,
        onlyFirstPage: true,
        maxHeightMode:true,
        selectDataNow:true,
        afterLoadTable:function(){
            //获取当前角色有哪些权限
            $().invoke(
                p_path+"/settings/prvlg/role/getMenuIdByRoleId.json",
                {"roleId":roleId},
                function(m,r){

                    if (isEmpty(r)){
                        return;
                    }
                    for (var i in r){
                        $("i[datav="+r[i]+"]",$("#menuList")).addClass("active");
                    }
                }
            )
        }
    })
}

function queryMenuList(){
    var param={"menuName":$("#queryMenuName").val()};
    $("#menuList").selectDataAfterSetParm(param);
}
function queryMenuListMan(){
    var param={"menuName":$("#queryMenuNameMan").val()};
    $("#dataTable").selectDataAfterSetParm(param);
}


function checkedMenuBox(obj,divid){
    //如果当前为空，那么说明当前的操作是要选中他。如果当前不为空，那么说明当前的操作是要取消选中
    var isChecked=$(obj).prop("class");
    var b=(isChecked=='active'?false:true);
    var parentId=$(obj).attr("otherdatav");
    var id=$(obj).attr("datav");

    if (b){
        $("i[datav="+parentId+"]",$("#"+divid)).addClass("active");
    }

}

function modifyRoleDiag(obj){
    //alert(($(obj).parentsUntil("table"))[0].outerHTML)
    var tr=obj.parentNode.parentNode.parentNode;
    var datat=$(tr).data("trData");
    //REMARK
    for (var key in datat){
        $("[data-name="+key+"]",$('#mod-js')).val(datat[key]);
        //$("textarea[data-name="+key+"]").html(datat[key]);
    }
    $('#mod-js').modal('show');
}
function modifyRole(){
    var datattt=$("#modRoleForm").serialize();
    var url=p_path+"/settings/prvlg/role/updateRole1.json"
    $().invoke(url,datattt,function(m,r){
        $("input[name='rId']",$('#mod-js')).val("");
        $('#mod-js').modal('hide');
        flashMessage_(r);
        $("#dataTable").refresh();
    })
}

//=======================menuManager====
function modifyMenuDiag(obj){
    var tr=obj.parentNode.parentNode.parentNode;
    var datat=$(tr).data("trData");
    for (var key in datat){
        $("[data-name="+key+"]",$('#modifyMenu')).val(datat[key]);
    }
    $('#modifyMenu').modal('show');
}
function modifyMenu(){
    var datattt=$("#modifyMenuForm").serialize();
    var url=p_path+"/settings/prvlg/menu/updateMenu1.json";
    $().invoke(url,datattt,function(m,r){
        $("input[name='id']",$('#modifyMenu')).val("");
        $('#modifyMenu').modal('hide');
        flashMessage_(r);
        $("#dataTable").refresh();
    })
}